﻿using BankSystem.BankingSystem;
using BankSystem;
using System;
using System.Windows.Forms;

namespace net第二次作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeBankAndAccounts();
        }

            private Bank bank = new Bank();
            private ATM atm = new ATM();

            private void InitializeBankAndAccounts()
            {
                var account1 = new Account(1, 10000); // 假设这里创建一个普通的Account实例
                var creditAccount1 = new CreditAccount(2, 10000, 5000);

                bank.AddAccount(account1);
                bank.AddAccount(creditAccount1);

                // 订阅 BigMoneyFetched 事件
                account1.BigMoneyFetched += Account_BigMoneyFetched;
                creditAccount1.BigMoneyFetched += Account_BigMoneyFetched;
            }

            private void Account_BigMoneyFetched(object sender, BigMoneyArgs e)
            {
                // 在界面中显示警告信息
                MessageBox.Show($"注意：有一大笔支出： {e.Amount:C}  账户为： {e.Account.id}.");
            }

            private void withdrawButton_Click(object sender, EventArgs e)
            {
                try
                {
                    var account = bank.GetAccount(Convert.ToInt32(accountIdTextBox.Text)); // 从银行提取账户
                    if (account == null)
                    {
                        MessageBox.Show("账户不存在");
                        return;
                    }

                    atm.Withdraw(account, Convert.ToDouble(withdrawAmountTextBox.Text));
                    MessageBox.Show($"取款成功！ 账户余额: {account.balance:C}");
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"错误: {ex.Message}"); //控制输入
                }
            }
    }
}
